<?php
namespace app\admin\controller;
use think\Controller;

class News extends Base
{

	public function index(){
		$data = input('param.');
		//生成 URL-encode 之后的请求字符串
		$query = http_build_query($data);
		$whereData=[];
		//转换查询条件
		if (!empty($data['start_time']) && !empty($data['end_time']) && $data['end_time'] > $data['start_time']) {
			$whereData['create_time'] = [
				['gt',strtotime($data['start_time'])],
				['lt',strtotime($data['end_time'])],
			];
		}
		if (!empty($data['catid'])) {
			$whereData['catid']=intval($data['catid']);
		}

		if (!empty($data['title'])) {
			$whereData['title']=['like','%'.$data['title'].'%'];
		}


		// 获取数据 填充到模版
		//模式一
		// $news = model('News')->getNews();

		//模式二
		//page  size   from   limit from size
		$this->getPageAndSize($data);
		//获取表里的数据
		$news=model('News')->getNewsByCondition($whereData,$this->form,$this->size);
		//获取满足条件的总数   => 多少页
		$total = model('News')->getNewsCountCondition();
		//结合总数和size => 有多少页
		$pageTotal = ceil($total/$this->size);

		return $this->fetch('',[
				'cats'=>config('cat.lists'),
				'news'=>$news,
				'count'=>$total,
				'pageTotal'=>$pageTotal,
				'curr'=>$this->page,
				'start_time'=>empty($data['start_time'])?'':$data['start_time'],
				'end_time'=>empty($data['end_time'])?'':$data['end_time'],
				'title'=>empty($data['title'])?'':$data['title'],
				'catid'=>empty($data['catid'])?'':$whereData['catid'],
				'curr'=>$this->page,
				'query'=>$query,
			]);
	}

	/**
	 * [add 添加文章]
	 */
	public function add(){
		if (request()->isPost()) {
			$data = input('post.');
			//数据校验 validate机制自动完成
			$validate = validate('News');
			if (!$validate->scene('add')->check($data)) {
                return $this->error($validate->getError());
            }
			//入库操作
			try {
				$id = model('News')->add($data);
			} catch (Exception $e) {
				return $this->result('',0,'新增失败');
			}

			if ($id) {
				return $this->result(['jump_url'=>url('news/index')],1,'ok');
			}else{
				return $this->result('',0,'新增失败');
			}
		}else{
			return $this->fetch('',[
					'cats'=>config('cat.lists'),
			]);
		}
	}


	/**
	 * [del 删除文章]
	 * @return [type] [description]
	 */
	public function del(){
	   return $this->delete(request()->param('id'));
	}

	/**
	 * [del 编辑文章  // 1、 id 获取记录所有数据 填充模板。  save([], ['id'=>$id])]
	 * @return [type] [description]
	 */
	public function edit(){
		if (request()->isPost()) {
			$data = input('post.');
			//数据校验 validate机制自动完成
			$validate = validate('News');
			if (!$validate->scene('edit')->check($data)) {
                return $this->error($validate->getError());
            }

            $condition['id']=$data['id'];
            unset($data['id']);
            //修改入库操作
			try {
				$id = model('News')->editConditionData($condition,$data);
			} catch (Exception $e) {
				return $this->result('',0,'修改失败');
			}

			if ($id) {
				return $this->success('修改成功','news/index');
			}else{
				return $this->result('',0,'修改失败');
			}


		}elseif (request()->isGet()) {
		   $whereData = ['id'=>request()->param('id')];
		   $originalInfo=model('News')->getNewsByCondition($whereData,0,1);
		   return $this->fetch('',[
						'cats'=>config('cat.lists'),
						'originalInfo'=>$originalInfo[0],
			]);
		}else{
			return $this->error('非法请求');
		}
	}
}
